<include file="header" />
<body>
<div class="wraper">
    <div class="head clearfix">
        <a href="javascript:history.back();" class="iconfont icon-zuo pull-left"></a>
        <span class="head-name pull-left">充值金额</span>
        <a href="{BEESCRM::U('/Base/Kitchen/index',array('kitchenId'=>$kitchenId))}" class="iconfont icon-shouye pull-left text-right"></a>
    </div>
    <div class="title padding tb-mode">
        <p class="tb-cell">请选择充值金额</p>
    </div>
    <div class="lists">
        <div class="time-box clearfix">
            <volist name="data['recharge']" id="vo" key="k">
                <div class="time-list <if condition='$k eq 1'> checked </if> " data-amount="{BEESCRM:$vo.value}" data-money="{BEESCRM:$vo.money_id}" data-hour="{BEESCRM:$vo.hour}">
                    <p>{BEESCRM:$vo.value}元</p>
                    <!-- <p class="text-green text-12">{BEESCRM:$vo.hour}小时</p> -->
                </div>
            </volist>
        </div>
    </div>


    <notempty name="chargingTips">
        <div class="title padding tb-mode">
            <p class="tb-cell">说明</p>
        </div>
        <div class="lists text-12">
            {BEESCRM:$chargingTips}
        </div>

        <div class="padding tb-mode">
            <p class="text-center text-grey text-12">充值即视为同意以上条款</p>
        </div>
    </notempty>


    <div class="btn-box fixed_div">
        <a href="javascript:void(0);" class="btn btn-submit">立即充值</a>
    </div>
</div>

<input type="hidden" name="hour" id="hour" data-money="" data-moneyid="" data-hour="" />

<include file="commonJS"/>


<script>
    seajs.use("$",function() {
        if($(document).height()>$(window).height()){
            $('.btn-box').addClass('fixed-btn');
            $('.wraper').css('paddingBottom',50);
        }else{
            $('.btn-box').removeClass('fixed-btn');
        }

        /**
         * 选择充值时间
         */
        $('.time-box').on('click','.time-list',function(){
            $(this).toggleClass('checked').siblings().removeClass('checked');
            /*var money=Number($(this).data('amount'));
            var moneyId=$(this).data('money');
            var hour=$(this).data('hour');
            $('#hour').attr('data-money',money);
            $('#hour').attr('data-moneyId',moneyId);
            $('#hour').attr('data-hour',hour);*/
        })
        /**
         * 充值
         */
        $(document).on('click','.btn-submit',function(){
           /* var hour = Number($('#hour').data('hour')); //对应的充电时间
            var amount = Number($('#hour').data('money')); //充值金额
            var money_id = $('#hour').data('moneyid'); //充值金额对应的moneyId*/
            var hour = Number($('.time-box').find('.checked').data('hour'));
            var amount = Number($('.time-box').find('.checked').data('amount'));
            var money_id = $('.time-box').find('.checked').data('money')

            console.log(hour);
            console.log(amount);
            console.log(money_id);
            //var ratio_num = Number($('#ratio_num').val());
            var amountReg = /^[0-9]*[1-9][0-9]*$/;
            if(amount == 0){
                fm.alert('充值金额不能为空');return ;
            }else if(!amountReg.test(amount)){
                fm.alert('充值金额只能为正整数');return ;
            }

            $.ajax({
                type:'post',
                dataType:'json',
                data:{'goods_amount':amount , 'money_id':money_id},
                url:"{BEESCRM::U('/base/KitchenReCharging/createOrder',array('kitchenId'=>$kitchenId))}",
                beforeSend: function(){
                    $('.load').show();
                },
                success:function(data){
                    console.log(data); $('.load').hide();
                    if(data.status == 'F'){
                        fm.alert(data.message);return ;
                    }else{
                        var id = data.data.order_id;
                        var wxpay = $.parseJSON(data.wxpay);
                        pay(wxpay, id);
                    }
                },
                error:function(){
                    $('.load').hide();
                    fm.alert(data.message);
                    return false;
                }
            });
        });

        //数据处理错误
        function show_error(data){
            fm.alert(data.message);
            /* if(data.url){
             setTimeout('location.href="'+data.url+'"',2000);
             } */
        }

        //微支付
        function pay(wxpay, id){
            if (typeof WeixinJSBridge == "undefined"){
                if( document.addEventListener ){
                    document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
                }else if (document.attachEvent){
                    document.attachEvent('WeixinJSBridgeReady', jsApiCall);
                    document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
                }
            }else{
                WeixinJSBridge.invoke(
                        'getBrandWCPayRequest',
                        wxpay,
                        function(res){
                            WeixinJSBridge.log(res.err_msg);
       
                            if(res.err_msg == "get_brand_wcpay_request:ok"){
                                //paySuccess(id);
                                location.href="{BEESCRM::U('base/KitchenReCharging/chargingMoneyRecord', array('kitchenId'=>$kitchenId))}";
                            }else if(res.err_msg == "get_brand_wcpay_request:cancel"){
                                fm.alert('付款已取消');
                                return false;
                            }else{
//							$.alert('error','调用微支付失败');
                                fm.alert(res.err_msg);
                                return false;
                            }
                        }
                );
            }
        }

        function paySuccess(order_id){
        
            url = "{BEESCRM::U('base/KitchenReCharging/paysuccess',array('kitchenId'=>$kitchenId))}/order_id/"+order_id;
            //$.getAjax(json,false,redirect);
            $.ajax({
                url:url,
                dataType:"json",
                beforeSend:function(){
                    fm.showLoging('正在完成..')
                },
                success:function(data){
                    fm.closeLayer();
                    redirect(data);
                },
                error:function(data){
                    fm.alert(data.msg)
                }
            })
        }

        function redirect(data){
            location.href = data.url;
        }
    });
</script>

</body>
</html>